package com.sdgakj.service.area;

import com.google.common.collect.Maps;
import com.sdgakj.entity.ShtCity;
import com.sdgakj.entity.ShtDistrict;
import com.sdgakj.entity.ShtProvince;
import com.sdgakj.repository.ShtCityDao;
import com.sdgakj.repository.ShtDistrictDao;
import com.sdgakj.repository.ShtProvinceDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Transactional
public class AreaService {
	
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private ShtCityDao shtCityDao;
	
	@Autowired
	private ShtProvinceDao shtProvinceDao;
	
	@Autowired
	private ShtDistrictDao shtDistrictDao;

	/**
	 * store shtCity=>list<sht_District>
	 */
	public static Map<Integer, List<ShtDistrict>> City_District_Cache = new ConcurrentHashMap<>();
	
	public Integer getProvinceIdByName(String name) {
		Integer id = 0;
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (!Objects.equals(null, name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<ShtProvince> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShtProvince.class);
		List<ShtProvince> provinceList = shtProvinceDao.findAll(spec);
		if (provinceList != null && provinceList.size() > 0) {
			id = provinceList.get(0).getId();
		}
		return id;
	}
	
	public Integer getCityIdByName(String name) {
		Integer id = 0;
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (!Objects.equals(null, name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<ShtCity> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShtCity.class);
		List<ShtCity> citys = shtCityDao.findAll(spec);
		if (citys != null && citys.size() > 0) {
			id = citys.get(0).getId();
		}
		return id;
	}
	
	public List<ShtProvince> getProvincesOrderByName() {
		return (List<ShtProvince>) shtProvinceDao.findAll();
	}
	
	public List<ShtProvince> getProvincesList(int pid, String pname) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (!Objects.equals(0, pid)) {
			filters.put("id", new SearchFilter("id", Operator.EQ, pid));
		} else if (!Objects.equals(null, pname)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, pname));
		}
		Specification<ShtProvince> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShtProvince.class);
		return shtProvinceDao.findAll(spec);
	}

	/***
	 *
	 * 根据城市名称模糊查询城市
	 *
	 * @param name
	 *
	 * @return
	 *
	 */

	public List<ShtCity> getCityByName(String name) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (!Objects.equals(null, name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<ShtCity> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShtCity.class);
		return shtCityDao.findAll(spec);
	}
	
	public List<ShtCity> getCityByNameAndPid(int pid, String name) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (!Objects.equals(0, pid)) {
			filters.put("province.id", new SearchFilter("province.id", Operator.EQ, pid));
		}
		if (!Objects.equals(null, name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<ShtCity> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShtCity.class);
		return shtCityDao.findAll(spec);
	}

	public List<ShtProvince> getProvinceOrderbyName() {
		return shtProvinceDao.getProvinceOrderbyName();
	}

	public List<String> getAllCitys(String city) {
		return shtCityDao.getAllCitys(city);
	}

	/**
	 * 查询检测场的城市列表
	 *
	 * @return
	 */
	public List<ShtCity> findCitys4Factory() {
		return shtCityDao.findCitys4Factory();
	}
	
}
